//============================================================
// Producnt name:		BoBoARTS.CodeMad
// Version: 			1.0
// Coded by:			Shen Bo (bo.shen@jb-aptech.com.cn)
// Auto generated at: 	2010-6-17 17:38:41
//============================================================

using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using WCSY.MODEL;

namespace WCSY.DAL
{
	public static partial class Mg_rightService
	{
        public static int AddMg_right(Mg_right mg_right)
		{
            string sql =
				"INSERT mg_rights (parentid, rightsname, rightstype, rightsurl, remarks)" +
				"VALUES (@parentid, @rightsname, @rightstype, @rightsurl, @remarks)";
				
			//sql += " ; SELECT @@IDENTITY";

            try
            {
				SqlParameter[] para = new SqlParameter[]
				{
					new SqlParameter("@parentid", mg_right.Parentid),
					new SqlParameter("@rightsname", mg_right.Rightsname),
					new SqlParameter("@rightstype", mg_right.Rightstype),
					new SqlParameter("@rightsurl", mg_right.Rightsurl),
					new SqlParameter("@remarks", mg_right.Remarks)
				};

                int issuc = DBHelper.GetScalar(sql, para);
              
				return issuc;
            }
            catch (Exception e)
            {
				Console.WriteLine(e.Message);
                throw e;
            }
		}
		
        public static void DeleteMg_right(Mg_right mg_right)
		{
			DeleteMg_rightByRightsid( mg_right.Rightsid );
		}

        public static void DeleteMg_rightByRightsid(int rightsid)
		{
            string sql = "DELETE mg_rights WHERE Rightsid = @Rightsid";

            try
            {
				SqlParameter[] para = new SqlParameter[]
				{
					new SqlParameter("@rightsid", rightsid)
				};
			
                DBHelper.ExecuteCommand(sql, para);
            }
            catch (Exception e)
            {
				Console.WriteLine(e.Message);
				throw e;
            }
		}
		
        public static void ModifyMg_right(Mg_right mg_right)
        {
            string sql =
                "UPDATE mg_rights " +
                "SET " +
	                "parentid = @parentid, " +
	                "rightsname = @rightsname, " +
	                "rightstype = @rightstype, " +
	                "rightsurl = @rightsurl, " +
	                "remarks = @remarks " +
                "WHERE rightsid = @rightsid";


            try
            {
				SqlParameter[] para = new SqlParameter[]
				{
					new SqlParameter("@rightsid", mg_right.Rightsid),
					new SqlParameter("@parentid", mg_right.Parentid),
					new SqlParameter("@rightsname", mg_right.Rightsname),
					new SqlParameter("@rightstype", mg_right.Rightstype),
					new SqlParameter("@rightsurl", mg_right.Rightsurl),
					new SqlParameter("@remarks", mg_right.Remarks)
				};

				DBHelper.ExecuteCommand(sql, para);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
				throw e;
            }

        }		

        public static IList<Mg_right> GetAllMg_rights()
        {
            string sqlAll = "SELECT * FROM mg_rights";
			return GetMg_rightsBySql( sqlAll );
        }
		
        public static Mg_right GetMg_rightByRightsid(int rightsid)
        {
            string sql = "SELECT * FROM mg_rights WHERE Rightsid = @Rightsid";

            try
            {
                SqlDataReader reader = DBHelper.GetReader(sql, new SqlParameter("@Rightsid", rightsid));
                if (reader.HasRows)
                {
                    Mg_right mg_right = new Mg_right();
                    if (reader.Read())
                    {
                       

                        mg_right.Rightsid = Convert.ToInt32(reader["rightsid"]);
                        mg_right.Parentid = Convert.ToInt32(reader["parentid"]);
                        mg_right.Rightsname = reader["rightsname"].ToString()+"";
                        mg_right.Rightstype = Convert.ToInt32(reader["rightstype"]);
                        mg_right.Rightsurl = reader["rightsurl"].ToString()+"";
                        mg_right.Remarks = reader["remarks"].ToString()+"";


                        if(reader.IsClosed==false){
                            reader.Close();
                        }
                       
                    }
                    return mg_right;
                }
                    else
                    {
                        reader.Close();
                        return null;
                    }
               
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                throw e;
            }
        }
		
		
		
		
        private static IList<Mg_right> GetMg_rightsBySql( string safeSql )
        {
            List<Mg_right> list = new List<Mg_right>();

            try
            {


                DataTable table = DBHelper.GetDataSet(safeSql);

                //System.Diagnostics.Debug.WriteLine("1234567890");
                
                    foreach (DataRow row in table.Rows)
                    {
                        Mg_right mg_right = new Mg_right();

                        mg_right.Rightsid = Convert.ToInt32(row["rightsid"]);
                        mg_right.Parentid = Convert.ToInt32(row["parentid"]);
                        mg_right.Rightsname = row["rightsname"].ToString();
                        mg_right.Rightstype = Convert.ToInt32(row["rightstype"]);
                        mg_right.Rightsurl = row["rightsurl"].ToString();
                        mg_right.Remarks = row["remarks"].ToString();

                        list.Add(mg_right);
                    }

                    
                
                return list;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);

                throw e;
            }

  }	
        private static IList<Mg_right> GetMg_rightsBySql( string sql, params SqlParameter[] values )
        {
            List<Mg_right> list = new List<Mg_right>();

			try
			{
				DataTable table = DBHelper.GetDataSet( sql, values );
				
				foreach (DataRow row in table.Rows)
				{
					Mg_right mg_right = new Mg_right();
					
					mg_right.Rightsid = (int)row["rightsid"];
					mg_right.Parentid = (int)row["parentid"];
					mg_right.Rightsname = (string)row["rightsname"];
					mg_right.Rightstype = (int)row["rightstype"];
					mg_right.Rightsurl = (string)row["rightsurl"];
					mg_right.Remarks = (string)row["remarks"];
	
					list.Add(mg_right);
				}
	
				return list;
			}
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                throw e;
            }
			
        }

        public  static IList<Mg_right> GetMg_rightsBySqlDataset(string sql) {

            return GetMg_rightsBySql(sql);
        
        }

        //public static List<Mg_right> GetMg_rightByParentid(int parentid)
        //{
        //    string sql = "SELECT * FROM mg_rights WHERE parentid = @parentId and rightstype=1";
        //    List<Mg_right> rbp = new List<Mg_right>();
        //    try
        //    {
        //        SqlDataReader reader = DBHelper.GetReader(sql, new SqlParameter("@parentId", parentid));

        //        while (reader.Read())
        //        {
        //            Mg_right mg_right = new Mg_right();

        //            mg_right.Rightsid = Convert.ToInt32(reader["rightsid"]);
        //            mg_right.Parentid = Convert.ToInt32(reader["parentid"]);
        //            mg_right.Rightsname = reader["rightsname"].ToString();
        //            mg_right.Rightstype = Convert.ToInt32(reader["rightstype"]);
        //            mg_right.Rightsurl = reader["rightsurl"].ToString();
        //            mg_right.Remarks = reader["remarks"].ToString();

        //            rbp.Add(mg_right);
        //            if(reader!=null){
        //            reader.Close();
                    
        //            }


        //        }

        //    }
        //    catch (Exception e)
        //    {
        //        Console.WriteLine(e.Message);
        //        throw e;
        //    }
           
            
            
        //    return rbp;
        //}
		
	}
}
